package com.example.xgakj.securityguard.ui.activities;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.xgakj.mylibrary.mvp.utils.SharedPreferencesUtil;
import com.example.xgakj.mylibrary.mvp.utils.ToastUtil;
import com.example.xgakj.securityguard.R;
import com.example.xgakj.securityguard.base.BaseActivity;
import com.example.xgakj.securityguard.base.BaseApplication;
import com.example.xgakj.securityguard.base.Parameters;
import com.qmuiteam.qmui.widget.QMUITopBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ServerIPActivity extends BaseActivity {

    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.et_IP)
    EditText etIP;
    @BindView(R.id.et_serverPort)
    EditText etServerPort;
    @BindView(R.id.et_pushPort)
    EditText etPushPort;
    @BindView(R.id.et_recordPort)
    EditText etRecordPort;
    @BindView(R.id.btn_save)
    Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_server_ip);
        ButterKnife.bind(this);
        initTopBar();
        initViews();
    }

    private void initViews() {
        etIP.setText(SharedPreferencesUtil.getString(mContex,Parameters.SERVER_IP,""));
        etServerPort.setText(SharedPreferencesUtil.getString(mContex,Parameters.SERVER_PORT,""));
        etRecordPort.setText(SharedPreferencesUtil.getString(mContex,Parameters.RECORD_PORT,""));
        etPushPort.setText(SharedPreferencesUtil.getString(mContex,Parameters.PUSH_PORT,""));
    }

    private void initTopBar() {
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

                overridePendingTransition(R.anim.slide_still, R.anim.slide_out_right);
            }
        });
        topbar.setTitle("配置");
    }

    @OnClick(R.id.btn_save)
    public void onViewClicked() {
        saveData();
        ToastUtil.showShort(mContex.getApplicationContext(),"保存成功");
    }

    private void saveData() {
        String ip = etIP.getText().toString().trim();
        SharedPreferencesUtil.setString(mContex, Parameters.SERVER_IP, ip);
        String recordPort = etRecordPort.getText().toString().trim();
        SharedPreferencesUtil.setString(mContex, Parameters.SERVER_PORT,etServerPort.getText().toString().trim());
        SharedPreferencesUtil.setString(mContex, Parameters.PUSH_PORT,etPushPort.getText().toString().trim());

        SharedPreferencesUtil.setString(mContex, Parameters.RECORD_PORT, recordPort);
        BaseApplication.baseUrl = "http://" + ip + ":" + recordPort + "/def.aspx?";

    }
}
